/*
*    Phantom v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
// String.h: interface for the String class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_STRING_H__05501DA0_17A9_4DF0_8A9C_933B7466B4D0__INCLUDED_)
#define AFX_STRING_H__05501DA0_17A9_4DF0_8A9C_933B7466B4D0__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <algorithm>


//#define __OLD_STRING__

#ifdef __OLD_STRING__

#include "ssi_defines.h"
#include "ssi_includes.h"
#include "ssi_tnode.h"

class SSI_SCRIPTDLL_API String;
typedef SSI_SCRIPTDLL_API TNodeTree<String> StringTree;
typedef SSI_SCRIPTDLL_API TNode<String> StringNode;

class SSI_SCRIPTDLL_API String  
{
private:
  SSI_CHAR* str;
public:

	void CopyToBuffer(SSI_CHAR* In) const;
	SSI_INT64 ReverseFind(const SSI_CHAR* In, SSI_INT64 StartPos = -1, SSI_INT64 StopPos = -1);
	void Reverse();
	void SetWithinState(SSI_CHAR StartChar, SSI_CHAR EndChar);
	void SetAfterState(SSI_CHAR Char);
	void SetBeforeState(SSI_CHAR Char);
	SSI_INT64 FindOneOf(const String& Str, SSI_INT64 Start = -1, SSI_INT64 Stop = -1) const;
	SSI_BOOL CheckChars(SSI_WORD wFlags) const;
	SSI_BOOL SetEndQuotes(SSI_BOOL bConvertExistingEnds = 0);
	SSI_CHAR chop(SSI_BOOL bFromStart = 0);
	SSI_BOOL CheckEndQuotes(SSI_BOOL bStrip = 0, SSI_BOOL bTrim = 0);
	void SetAt(SSI_INT64 Pos, SSI_CHAR c);
	SSI_INT64 Format(const SSI_CHAR* fmt, ...);
	void SetQuoteState(SSI_BOOL bSet);
	SSI_CHAR GetAt(SSI_INT64 Pos) const;
	void Trim();
	void TrimRight();
	void TrimLeft();
	SSI_INT64 Parse(String& Delimeters, StringTree& Return, SSI_INT64 DelLen);
	SSI_INT64 Parse(String& Delimeters, String &Return, String* Tok = SSI_NULL, SSI_INT64 DelLen = -1);
	SSI_INT64 Parse(const char* Delimeters, String &Return, String* Tok = SSI_NULL, SSI_INT64 DelLen = -1){String sTemp = Delimeters; return Parse(sTemp,Return,Tok,DelLen);};
	String Right(SSI_INT64 NumChars) const;
	String Left(SSI_INT64 NumChars) const;
  SSI_INT64 Find(const SSI_CHAR* In, SSI_INT64 StartPos = -1, SSI_INT64 StopPos = -1) const;
  SSI_INT64 Find(const String &In, SSI_INT64 StartPos = -1, SSI_INT64 StopPos = -1) const;
	String SubString(SSI_INT64 Start, SSI_INT64 Finish = -1) const;
	SSI_INT64 Replace(const SSI_CHAR* Out, const SSI_CHAR* In);
	SSI_INT64 GetLength() const;
	SSI_BOOL operator ==(String &Other) const;
  SSI_BOOL operator ==(const SSI_CHAR* Other) const;
  String& operator=(const String &Other);
  String& operator=(const SSI_CHAR* Other);
  String operator+(String s2);
  String operator+(SSI_CHAR* s2);
	bool operator<(const String& Other) const;
	bool operator<(const SSI_CHAR* Other) const;
//	SSI_CHAR& operator [](SSI_INT64 i);
  operator CSSI_CHAR*() const {return str;};
  const SSI_CHAR* Str() const;
	static SSI_INT64 GetSize(const SSI_CHAR* InStr);
	
  String();
  String(const SSI_CHAR* InitVal); 
  String(SSI_CHAR InitChar);
  String(const String& Other);
	virtual ~String();
protected:
private:
	static SSI_INT64 loc_copy(const SSI_CHAR* src, SSI_CHAR* dest);
	SSI_CHAR* Null();
  //SSI_BYTE CHECK_QUOTE_STATE;
  SSI_CHAR CHECK_WITHIN_SSI_CHAR_START;
  SSI_CHAR CHECK_WITHIN_SSI_CHAR_END;
  SSI_CHAR CHECK_BEFORE_SSI_CHAR;
  SSI_CHAR CHECK_AFTER_SSI_CHAR;
public:
	SSI_BOOL IsNumber(void);
	bool IsWhiteSpace(void);
	SSI_BOOL GetQuoteState(void) const;
};

//////////////////////////////////////////////////////////////////////
#else

#include "ssi_defines.h"
#include "ssi_includes.h"
#include "ssi_tnode.h"

#include <string>
#include <algorithm>
using std::string;
using std::reverse;

class SSI_SCRIPTDLL_API String;
typedef SSI_SCRIPTDLL_API TNodeTree<String> StringTree;
typedef SSI_SCRIPTDLL_API TNode<String> StringNode;

class SSI_SCRIPTDLL_API String  
{
private:
  string str;
public:

	void CopyToBuffer(SSI_CHAR* In) const;
	SSI_INT64 ReverseFind(const SSI_CHAR* In, SSI_INT64 StartPos = -1);
	void Reverse();
	void SetWithinState(SSI_CHAR StartChar, SSI_CHAR EndChar);
	void SetAfterState(SSI_CHAR Char);
	void SetBeforeState(SSI_CHAR Char);
	SSI_INT64 FindOneOf(const String& Str, SSI_INT64 Start = -1) const;
	SSI_BOOL CheckChars(SSI_WORD wFlags) const;
	SSI_BOOL SetEndQuotes(SSI_BOOL bConvertExistingEnds = 0);
	SSI_CHAR chop(SSI_BOOL bFromStart = 0);
	SSI_BOOL CheckEndQuotes(SSI_BOOL bStrip = 0, SSI_BOOL bTrim = 0);
	SSI_INT64 SetAt(SSI_INT64 Pos, SSI_CHAR c);
	SSI_INT64 Format(const SSI_CHAR* fmt, ...);
	void SetQuoteState(SSI_BOOL bSet);
	SSI_CHAR GetAt(SSI_INT64 Pos) const;
	void Trim();
	void TrimRight();
	void TrimLeft();
	SSI_INT64 Parse(String& Delimeters, StringTree& Return, SSI_INT64 DelLen);
	SSI_INT64 Parse(String& Delimeters, String &Return, String* Tok = SSI_NULL, SSI_INT64 DelLen = -1);
	SSI_INT64 Parse(const char* Delimeters, String &Return, String* Tok = SSI_NULL, SSI_INT64 DelLen = -1){String sTemp = Delimeters; return Parse(sTemp,Return,Tok,DelLen);};
	String Right(SSI_INT64 NumChars) const;
	String Left(SSI_INT64 NumChars) const;
	//String Left(SSI_INT64 NumChars);
  SSI_INT64 Find(const SSI_CHAR* In, SSI_INT64 StartPos = -1, SSI_INT64 StopPos = -1) const;
  SSI_INT64 Find(const String &In, SSI_INT64 StartPos = -1, SSI_INT64 StopPos = -1) const;
	String SubString(SSI_INT64 Start, SSI_INT64 Finish = -1) const;
	void Replace(const SSI_CHAR* Out, const SSI_CHAR* In);
	static void replace(string& s, const SSI_CHAR* Out, const SSI_CHAR* In);
	SSI_INT64 GetLength() const;
	SSI_BOOL operator ==(String &Other) const;
  SSI_BOOL operator ==(const SSI_CHAR* Other) const;
  String& operator=(const String& Other);
  String& operator=(const SSI_CHAR* Other);
  String operator+(const String& s2);
  String operator+(SSI_CHAR* s2);
	bool operator<(const String& Other) const;
	bool operator<(const SSI_CHAR* Other) const;
//	SSI_CHAR& operator [](SSI_INT64 i);
	operator CSSI_CHAR*() const {return str.c_str();};
  const SSI_CHAR* Str() const;
	static SSI_INT64 GetSize(const SSI_CHAR* InStr);
	
  String();
  String(const SSI_CHAR* InitVal); 
  String(SSI_CHAR InitChar);
  String(const String& Other);
	virtual ~String();
protected:
private:
	static SSI_INT64 loc_copy(const SSI_CHAR* src, SSI_CHAR* dest);
	SSI_CHAR* Null();
  //SSI_BYTE CHECK_QUOTE_STATE;
  SSI_CHAR CHECK_WITHIN_SSI_CHAR_START;
  SSI_CHAR CHECK_WITHIN_SSI_CHAR_END;
  SSI_CHAR CHECK_BEFORE_SSI_CHAR;
  SSI_CHAR CHECK_AFTER_SSI_CHAR;
public:
	SSI_BOOL IsNumber(void);
	bool IsWhiteSpace(void);
	SSI_BOOL GetQuoteState(void) const;
  void ToUpper(void);
  void ToLower(void);

};

#endif



#endif // !defined(AFX_STRING_H__05501DA0_17A9_4DF0_8A9C_933B7466B4D0__INCLUDED_)



